﻿
using Abp.Application.Services.Dto;
using Abp.Web.Models;
using Abp.Web.Mvc.Authorization;
using KDS.Mes.Authorization;
using KDS.Mes.Mes.Cartypes;
using KDS.Mes.Mes.Cartypes.Dtos;
using KDS.Mes.Mes.SeatInfos;
using KDS.Mes.Mes.SeatInfos.Dtos;
using KDS.Mes.Mes.SeatPositions;
using KDS.Mes.Mes.SeatPositions.Dtos;
using KDS.Mes.Mes.Styles;
using KDS.Mes.Mes.Styles.Dtos;
using KDS.Mes.Web.Models.SeatInfos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace KDS.Mes.Web.Controllers
{
    public class SeatInfosController : MesControllerBase
    {

        private readonly ISeatInfoAppService _seatInfoAppService;
        private readonly ICartypeAppService _cartypeAppService;
        private readonly IStyleAppService _styleAppService;
        private readonly ISeatPositionAppService _seatPositionAppService;

        public SeatInfosController(ISeatInfoAppService seatInfoAppService,
                                  ICartypeAppService cartypeAppService,
                                IStyleAppService styleAppService,
            ISeatPositionAppService seatPositionAppService)
        {
            _seatInfoAppService = seatInfoAppService;
            _cartypeAppService = cartypeAppService;
            _styleAppService = styleAppService;
            _seatPositionAppService = seatPositionAppService;

        }

        public async Task<ActionResult> Index()
        {
            var cartypes = new List<ComboboxItemDto>();
            var getCartypesOutput = await _cartypeAppService.GetCartypes(new GetCartypeInput());
            cartypes = getCartypesOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.CartypeName)).ToList();

            var styles = new List<ComboboxItemDto>();
            var getStylesOutput = await _styleAppService.Getstyles(new GetStyleInput());
            styles = getStylesOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.StyleName)).ToList();

            var seatPositions = new List<ComboboxItemDto>();
            var getSeatPositionOutput = await _seatPositionAppService.GetSeatPositions(new GetSeatPositionInput());
            seatPositions = getSeatPositionOutput.Items.Select(r => new ComboboxItemDto(r.Id.ToString(), r.SeatPositionName)).ToList();

            cartypes.Insert(0, new ComboboxItemDto("", ""));
            styles.Insert(0, new ComboboxItemDto("", ""));
            //seatPositions.Insert(0, new ComboboxItemDto("", ""));
            var model = new SeatInfosViewModel
            {
                FilterText = Request.QueryString["filterText"],
                Cartypes = cartypes,
                Styles = styles,
                SeatPositions = seatPositions
            };

            return View(model);
        }

        [DontWrapResult]
        public JsonResult GetAllSeatInfos(int limit, int offset, string sortfiled, string sortway,
                                        string cartypeId, string styleId, int[] seatPositions
                                        , string alccode)
        {
            var sort = !string.IsNullOrEmpty(sortfiled) ? string.Format("{0} {1}", sortfiled, sortway) : "";
            var filter = new GetSeatInfoInput
            {
                SkipCount = offset,
                MaxResultCount = limit,
                Sorting = sort,
                CartypeId = cartypeId,
                StyleId = styleId,
                SeatPositions = seatPositions,
                AlcCode = alccode
            };
            var pagedTasks = _seatInfoAppService.GetPagedSeatInfos(filter);
            return Json(new { total = pagedTasks.TotalCount, rows = pagedTasks.Items }, JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 根据id获取进行编辑或者添加的用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [AbpMvcAuthorize(AppPermissions.Pages_Bom_SeatInfos_Create, AppPermissions.Pages_Bom_SeatInfos_Edit)]
        public async Task<PartialViewResult> CreateOrEditSeatInfoModal(int? id)
        {
		var input=new NullableIdDto<int>{Id=id};
	 
                 var output=    await _seatInfoAppService.GetSeatInfoForEditAsync(input);

				 var viewModel=new CreateOrEditSeatInfoModalViewModel(output);


            return PartialView("_CreateOrEditSeatInfoModal",viewModel);
        }
	 
       
    }
}